using Microsoft.AspNetCore.Http;

namespace MyBlogServer.Services
{
    public static class SessionExtensions
    {
        private const string IsAuthenticatedKey = "IsAuthenticated";
        private const string UsernameKey = "Username";
        
        public static void SetAuthenticated(this ISession session, string username)
        {
            session.SetString(IsAuthenticatedKey, "true");
            session.SetString(UsernameKey, username);
        }
        
        public static void SetUnauthenticated(this ISession session)
        {
            session.Remove(IsAuthenticatedKey);
            session.Remove(UsernameKey);
        }
        
        public static bool IsAuthenticated(this ISession session)
        {
            return session.GetString(IsAuthenticatedKey) == "true";
        }
        
        public static string GetUsername(this ISession session)
        {
            return session.GetString(UsernameKey) ?? string.Empty;
        }
    }
}